package jdk8的特性.函数式接口.过滤式案例;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：idea
 * 日期：2018/8/19
 * 描述：使用函数式编程来优化代码内容
 */
public class Test2 {

    public static void main(String[] args) {
        Apple apple1=new Apple("red",11);
        Apple apple2=new Apple("blue",15);
        Apple apple3=new Apple("red",15);
        List<Apple> appleList=new ArrayList<>();
        appleList.add(apple1);
        appleList.add(apple2);
        appleList.add(apple3);

        List<Apple> resultList=new ArrayList<>();
        for (Apple apple : appleList) {
            if(appleFilter(apple,(apple4)->"red".equals(apple.getColor())&&(apple.getWeight()==15))){
                resultList.add(apple);
            }
        }
        resultList.forEach(x->{
            System.out.println(x.toString());
        });

    }


    //我们称之为谓词 一种返回boolean类型的函数
    public static boolean appleFilter(Apple apple,AppleFilter appleFilter){
        return appleFilter.test(apple);
    }
}
